package com.j180.dao.order;

import com.j180.bean.Order;
import org.apache.ibatis.annotations.*;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface IOrderDao {
    /**
     * 添加一个订单
     * @param order 订单类
     * @return 1 成功， 0 失败
     */
//<<<<<<< HEAD
//    @Insert("insert into t_order values(null, #{money}, #{comment.id}, #{petOwner.id}, #{store.id}, #{createTime})")
//=======
    @Insert("insert into t_order values(null,#{createTime},#{money}, #{comment.id}, #{petOwner.id}, #{store.id})")
    int insertOrder(Order order);

    /**
     * 根据门店编号查询订单
     * @param storeId 门店编号
     * @return 订单集合
     */
    @Select("SELECT\n" +
            "\tord.o_id,\n" +
            "\tord.o_createTime,\n" +
            "\tord.o_money,\n" +
            "\tcomm.c_id,\n" +
            "\tcomm.c_time,\n" +
            "\tcomm.c_score,\n" +
            "\tcomm.c_content,\n" +
            "\tcomm.c_pic,\n" +
            "\tcomm.c_type,\n" +
            "\tpetowner.p_id,\n" +
            "\tpetowner.p_telNum,\n" +
            "\tpetowner.p_nickname,\n" +
            "\tpetowner.p_realName,\n" +
            "\tpetowner.p_vipNum,\n" +
            "\tpetowner.p_pic,\n" +
            "\tpetowner.p_address,\n" +
            "\tpetowner.p_region,\n" +
            "\tpetowner.p_integral,\n" +
            "\tpetowner.p_petName,\n" +
            "\tstore.st_id,\n" +
            "\tstore.st_name,\n" +
            "\tstore.st_licenseNum,\n" +
            "\tstore.st_licensePic,\n" +
            "\tstore.st_address,\n" +
            "\tstore.st_location,\n" +
            "\tstore.st_city,\n" +
            "\tstore.st_corporation,\n" +
            "\tstore.st_telNum,\n" +
            "\tstore.st_photo,\n" +
            "\tstore.st_feature,\n" +
            "\tstore.st_vipLevel,\n" +
            "\tstore.st_commissionRate,\n" +
            "\tstore.st_clerkAttri,\n" +
            "\tdetail.d_id,\n" +
            "\tdetail.d_gNum,\n" +
            "\tdetail.d_sNum\n" +
            " FROM\n" +
            "\tt_order AS ord\n" +
            "LEFT JOIN t_comment AS comm ON ord.o_c_id = comm.c_id\n" +
            "LEFT JOIN t_petowner AS petowner ON ord.o_p_id = petowner.p_id\n" +
            "LEFT JOIN t_store AS store ON ord.o_st_id = store.st_id\n" +
            "LEFT JOIN t_detail AS detail ON detail.d_o_id = ord.o_id where ord.o_st_id =#{storeId}")
    @ResultMap("orderResult.orderMap")
    List<Order> getOrdersByStoreId(@Param("storeId") int storeId);

    /**
     * 根据宠物主人id查询订单
     * @param ownerId 宠主id
     * @return 订单集合
     */

    @Select("SELECT\n" +
            "\tord.o_id,\n" +
            "\tord.o_createTime,\n" +
            "\tord.o_money,\n" +
            "\tcomm.c_id,\n" +
            "\tcomm.c_time,\n" +
            "\tcomm.c_score,\n" +
            "\tcomm.c_content,\n" +
            "\tcomm.c_pic,\n" +
            "\tcomm.c_type,\n" +
            "\tpetowner.p_id,\n" +
            "\tpetowner.p_telNum,\n" +
            "\tpetowner.p_nickname,\n" +
            "\tpetowner.p_realName,\n" +
            "\tpetowner.p_vipNum,\n" +
            "\tpetowner.p_pic,\n" +
            "\tpetowner.p_address,\n" +
            "\tpetowner.p_region,\n" +
            "\tpetowner.p_integral,\n" +
            "\tpetowner.p_petName,\n" +
            "\tstore.st_id,\n" +
            "\tstore.st_name,\n" +
            "\tstore.st_licenseNum,\n" +
            "\tstore.st_licensePic,\n" +
            "\tstore.st_address,\n" +
            "\tstore.st_location,\n" +
            "\tstore.st_city,\n" +
            "\tstore.st_corporation,\n" +
            "\tstore.st_telNum,\n" +
            "\tstore.st_photo,\n" +
            "\tstore.st_feature,\n" +
            "\tstore.st_vipLevel,\n" +
            "\tstore.st_commissionRate,\n" +
            "\tstore.st_clerkAttri,\n" +
            "\tdetail.d_id,\n" +
            "\tdetail.d_gNum,\n" +
            "\tdetail.d_sNum\n" +
            " FROM\n" +
            "\tt_order AS ord\n" +
            "LEFT JOIN t_comment AS comm ON ord.o_c_id = comm.c_id\n" +
            "LEFT JOIN t_petowner AS petowner ON ord.o_p_id = petowner.p_id\n" +
            "LEFT JOIN t_store AS store ON ord.o_st_id = store.st_id\n" +
            "LEFT JOIN t_detail AS detail ON detail.d_o_id = ord.o_id where ord.o_p_id =#{ownerId}")
    @ResultMap("orderResult.orderMap")
    List<Order> getOrdersByPetOwnerId(@Param("ownerId") int ownerId);

    /**
     * 根据评价id得到订单
     * @param commentId 评价id
     * @return 订单集合
     */
    @Select("SELECT\n" +
            "\tord.o_id,\n" +
            "\tord.o_createTime,\n" +
            "\tord.o_money,\n" +
            "\tcomm.c_id,\n" +
            "\tcomm.c_time,\n" +
            "\tcomm.c_score,\n" +
            "\tcomm.c_content,\n" +
            "\tcomm.c_pic,\n" +
            "\tcomm.c_type,\n" +
            "\tpetowner.p_id,\n" +
            "\tpetowner.p_telNum,\n" +
            "\tpetowner.p_nickname,\n" +
            "\tpetowner.p_realName,\n" +
            "\tpetowner.p_vipNum,\n" +
            "\tpetowner.p_pic,\n" +
            "\tpetowner.p_address,\n" +
            "\tpetowner.p_region,\n" +
            "\tpetowner.p_integral,\n" +
            "\tpetowner.p_petName,\n" +
            "\tstore.st_id,\n" +
            "\tstore.st_name,\n" +
            "\tstore.st_licenseNum,\n" +
            "\tstore.st_licensePic,\n" +
            "\tstore.st_address,\n" +
            "\tstore.st_location,\n" +
            "\tstore.st_city,\n" +
            "\tstore.st_corporation,\n" +
            "\tstore.st_telNum,\n" +
            "\tstore.st_photo,\n" +
            "\tstore.st_feature,\n" +
            "\tstore.st_vipLevel,\n" +
            "\tstore.st_commissionRate,\n" +
            "\tstore.st_clerkAttri,\n" +
            "\tdetail.d_id,\n" +
            "\tdetail.d_gNum,\n" +
            "\tdetail.d_sNum\n" +
            " FROM\n" +
            "\tt_order AS ord\n" +
            "LEFT JOIN t_comment AS comm ON ord.o_c_id = comm.c_id\n" +
            "LEFT JOIN t_petowner AS petowner ON ord.o_p_id = petowner.p_id\n" +
            "LEFT JOIN t_store AS store ON ord.o_st_id = store.st_id\n" +
            "LEFT JOIN t_detail AS detail ON detail.d_o_id = ord.o_id where ord.o_c_id =#{commentId}")
    @ResultMap("orderResult.orderMap")
    List<Order> getOrdersByCommentId(@Param("commentId") int commentId);

    /**
     * 根据门店编号删除订单
     * @param storeId 门店编号
     * @return 1 成功， 0 失败
     */
    @Delete("delete from t_order where o_st_id =#{storeId}")
    int deleteOrderByStoreId(@Param("storeId") int storeId);

    /**
     * 根据宠主编号删除订单
     * @param ownerId 门店编号
     * @return 1 成功， 0 失败
     */

    @Delete("delete from t_order where o_p_id =#{ownerId}")
    int deleteOrdersByPetOwnerId(@Param("ownerId") int ownerId);


    /**
     * 根据评价编号删除订单
     * @param commentId 门店编号
     * @return 1 成功， 0 失败
     */
    @Delete("delete from t_order where o_c_id =#{commentId}")
    int deleteOrdersByCommentId(@Param("commentId") int commentId);

    /**
     * 修改订单信息
     * @param order 订单信息
     * @return 1 成功， 0 失败
     */

    @Update("update t_order set o_createTime=#{createTime},o_money=#{money} where o_id =#{id}")
    int updateOrder(Order order);

    @Select("SELECT\n" +
            "\tord.o_id,\n" +
            "\tord.o_createTime,\n" +
            "\tord.o_money,\n" +
            "\tcomm.c_id,\n" +
            "\tcomm.c_time,\n" +
            "\tcomm.c_score,\n" +
            "\tcomm.c_content,\n" +
            "\tcomm.c_pic,\n" +
            "\tcomm.c_type,\n" +
            "\tpetowner.p_id,\n" +
            "\tpetowner.p_telNum,\n" +
            "\tpetowner.p_nickname,\n" +
            "\tpetowner.p_realName,\n" +
            "\tpetowner.p_vipNum,\n" +
            "\tpetowner.p_pic,\n" +
            "\tpetowner.p_address,\n" +
            "\tpetowner.p_region,\n" +
            "\tpetowner.p_integral,\n" +
            "\tpetowner.p_petName,\n" +
            "\tstore.st_id,\n" +
            "\tstore.st_name,\n" +
            "\tstore.st_licenseNum,\n" +
            "\tstore.st_licensePic,\n" +
            "\tstore.st_address,\n" +
            "\tstore.st_location,\n" +
            "\tstore.st_city,\n" +
            "\tstore.st_corporation,\n" +
            "\tstore.st_telNum,\n" +
            "\tstore.st_photo,\n" +
            "\tstore.st_feature,\n" +
            "\tstore.st_vipLevel,\n" +
            "\tstore.st_commissionRate,\n" +
            "\tstore.st_clerkAttri,\n" +
            "\tdetail.d_id,\n" +
            "\tdetail.d_gNum,\n" +
            "\tdetail.d_sNum\n" +
            " FROM\n" +
            "\tt_order AS ord\n" +
            "LEFT JOIN t_comment AS comm ON ord.o_c_id = comm.c_id\n" +
            "LEFT JOIN t_petowner AS petowner ON ord.o_p_id = petowner.p_id\n" +
            "LEFT JOIN t_store AS store ON ord.o_st_id = store.st_id\n" +
            "LEFT JOIN t_detail AS detail ON detail.d_o_id = ord.o_id where ord.o_id =#{id}")
    @ResultMap("orderResult.orderMap")
    Order getOrderDetailInfoById(@Param("id") int id);

}
